// import { useState } from "react";
// import { LoginView } from "./login";
// import { RegisterView } from "./register";
// import { Button, Card } from "antd";
// export const LoginIndexView = () => {
//   const [isRegister, setIsRegister] = useState(false);

//   return (
//     <div style={{ display: "flex", justifyContent: "center" }}>
//       <Card>
//         {isRegister ? <RegisterView /> : <LoginView />}
//         <Button onClick={() => setIsRegister(!isRegister)}>
//           切换{isRegister ? "登录" : "注册"}
//         </Button>
//       </Card>
//     </div>
//   );
// };
import { useState } from "react";
import { LoginView } from "./login";
import { RegisterView } from "./register";
import { Button, Card, Divider, Typography } from "antd";
import styled from "@emotion/styled";
import logoIcon from "@/assets/logo.svg";
import leftIcon from "@/assets/left.svg";
import rightIcon from "@/assets/right.svg";
import { useDocumentTitle } from "../../hooks";
import { useUndo, useUndo1 } from "../../utils/use-undo";
import { ErrorBox } from "../../components/ErrorBox";
export const LoginIndexView = () => {
  const [isRegister, setIsRegister] = useState(false);
  const [error, setError] = useState<Error | null>(null);
  // useDocumentTitle(isRegister ? "注册" : "登录")

  const onChangeIsRegister = () => {
    setError(null);
    setIsRegister(!isRegister);
  };
  return (
    <Container>
      <Background />
      <Header />
      <ShadowCard>
        <Title>{isRegister ? "请注册" : "请登录"}</Title>
        {/* {error ? (
          <Typography.Text type="danger">{error?.message}</Typography.Text>
        ) : null} */}
        <ErrorBox error={error} />
        {isRegister ? (
          <RegisterView onError={setError} />
        ) : (
          <LoginView onError={setError} />
        )}
        <Divider />
        <Button type="link" onClick={onChangeIsRegister}>
          {isRegister ? "已有账号了？直接登录" : "没有账号？注册新账号"}
        </Button>
        {/* <TestUndo /> */}
      </ShadowCard>
    </Container>
  );
};

/**
 * @description: 通过emotion以js写样式的方式写组件
 * - styled.普通组件`样式`
 */
const Container = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
`;
/**
 * @description: 重写card的样式
 * - styled(组件)`样式`
 */
const ShadowCard = styled(Card)`
  width: 40rem;
  min-height: 56rem;
  padding: 3.2rem 4rem;
  border-radius: 0.3rem;
  box-sizing: border-box;
  box-shadow: rgba(0, 0, 0, 0.1) 0 0 10px;
  text-align: center;
`;

/**
 * @description: 定义标题组件
 */
const Header = styled.header`
  background: url(${logoIcon}) no-repeat center;
  padding: 5rem 0;
  background-size: 8rem;
  width: 100%;
`;

/**
 * @description: 标题
 */
const Title = styled.h2`
  margin-bottom: 2.4rem;
  color: rgb(94, 108, 132);
`;

/**
 * @description: 添加背景图片
 */
const Background = styled.div`
  position: absolute;
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: left bottom, right bottom;
  background-size: calc(((100vw - 40rem) / 2) - 3.2rem),
    calc(((100vw - 40rem) / 2) - 3.2rem), cover;
  background-image: url(${leftIcon}), url(${rightIcon});
`;

/**
 * @description: 测试undo组件
 * @return {*}
 */
const TestUndo1 = () => {
  const [{ current, canBack, canForward }, { undo, redo, set, reset }] =
    useUndo1(0);
  return (
    <div>
      <div>当前值：{current}</div>
      <button onClick={() => set(current + 1)}>+</button>
      <button onClick={() => set(current - 1)}>-</button>
      <button onClick={undo} disabled={!canBack}>
        后退
      </button>
      <button onClick={redo} disabled={!canForward}>
        前进
      </button>
      <button onClick={() => reset()}>重置</button>
    </div>
  );
};
const TestUndo = () => {
  const [{ current }, { undo, redo, set, reset, canBack, canForward }] =
    useUndo(0);
  return (
    <div>
      <div>当前值：{current}</div>
      <button onClick={() => set(current + 1)}>+</button>
      <button onClick={() => set(current - 1)}>-</button>
      <button onClick={undo} disabled={!canBack}>
        后退
      </button>
      <button onClick={redo} disabled={!canForward}>
        前进
      </button>
      <button onClick={() => reset()}>重置</button>
    </div>
  );
};
